﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYue.Permission.BLL;
using XinYue.Permission.Mode;
using XinYue.Permission.Web.Models;

namespace XinYue.Permission.Web.Controllers
{
    public class AdminUserController : Controller
    {
        /// <summary>
        /// 实例业务逻辑层
        /// </summary>
        private AdminUserService adminUserService = new AdminUserService();

        #region 多表添加或多表修改  AdminUser  AdminUserRole  ModuleUser
        /// <summary>
        /// 多表添加  AdminUser  AdminUserRole  ModuleUser
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult AddAdminUser(AdminUser adminUser, int roleId, int moudleId)
        {
            Operate operate = new Operate();
            //管理员ID为空则为添加 否则修改
            if (adminUser.Id == 0)
            {
                operate.Success = adminUserService.Add(adminUser, roleId, moudleId);
                if (operate.Success) operate.Context = "管理员添加成功";
            }
            else
            {
                operate.Success = adminUserService.UpdateAdminUser(adminUser, roleId, moudleId);
                if (operate.Success) operate.Context = "管理员修改成功";
            }
            return Json(operate);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult DelAdminUser(AdminUser adminUser)
        {
            Operate operate = new Operate();
            operate.Success = adminUserService.DelAdminUser(adminUser);
            return Json(operate);
        }
        #endregion

        #region 修改管理员状态
        /// <summary>
        /// 修改管理员状态
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult UpdateStatus(int id,int status)
        {
            Operate operate = new Operate();
            //因为修改会将所有值修改 所以修改状态之前根据id拿到要修改的整个对象
            Expression<Func<AdminUser, bool>> Wherelambda = c => c.Id == id;
            AdminUser adminUser=adminUserService.Query(Wherelambda);
            adminUser.Enabled = status>0;
            operate.Success = adminUserService.Update(adminUser);
            return Json(operate);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteByID(string AdminID)
        {
            Operate operate = new Operate();
            operate.Success = adminUserService.BatchDeleteByID(AdminID);
            return Json(operate);
        }
        #endregion

        #region 根据所选组ID查询角色名称
        /// <summary>
        /// 根据所选组ID查询角色名称
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult SelRoleByModulID(string data)
        {
            List<RoleByModule> roleByModules= adminUserService.SelRoleByModulID(data);
            return Json(roleByModules);
        }
        #endregion

        #region 查询分页
        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        public JsonResult GetAdminUserPageList(int page, int limit,string RoleName,string data,string AdminName)
        {
            List<AdminUserQuery> adminUserQueries = adminUserService.GetAdminUserQueryList(RoleName, data, AdminName);
            PageListResult<AdminUserQuery> pageListResult = new PageListResult<AdminUserQuery>();
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            pageListResult.count = adminUserQueries.Count;
            //Skip()方法用于查到指定条记录条数时跳出 参数：(page-1)*10 也就是‘规律’
            //Take()方法用于查到指定的条数时将其取出 参数：每页想要显示的条数记录limit
            //int count = 0;
            //Expression<Func<AdminUser, bool>> Wherelambda = c => c.Name.Contains(name);
            //Expression<Func<AdminUser, int>> OrderBylambda = c => c.Id;
            pageListResult.data = adminUserQueries.Skip((page - 1) * limit).Take(limit).ToList();
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}